// 限制子节点的移动
<template>
  <el-button @click="graphEmbed" type="primary" style="margin: 10px">嵌套</el-button>
  <div id="container"></div>
</template>

<script lang="ts" setup>
import { ElMessage } from "element-plus";
import { Graph, DataUri, Shape } from "@antv/x6";
import { onMounted, ref } from "vue";

let graph = null;

const graphEmbed = () => {
  ElMessage({
    message: "嵌套",
    type: "success",
  });
  const child = graph.addNode({
    x: 120,
    y: 80,
    width: 120,
    height: 60,
    zIndex: 10,
    label: "Child\n(embedded)",
    attrs: {
      body: {
        fill: "green",
      },
      label: {
        fill: "#fff",
      },
    },
  });

  const parent = graph.addNode({
    x: 80,
    y: 40,
    width: 320,
    height: 240,
    zIndex: 1,
    label: "Parent\n(try to move me)",
  });

  parent.addChild(child);
};

const renderGraph = () => {
  graph = new Graph({
    container: document.getElementById("container"),
    width: 800,
    height: 600,
    panning: true,
    mousewheel: true,
    // 设置画布背景颜色
    background: {
      color: "#F2F7FA",
    },
    translating: {
      restrict(view) {
        const cell = view.cell;
        if (cell.isNode()) {
          const parent = cell.getParent();
          if (parent) {
            return parent.getBBox();
          }
        }

        return null;
      },
    },
  });

  //创建流程图数据
  const data = {
    //节点数据
    nodes: [],
  };

  graph.fromJSON(data); // 渲染元素
  graph.centerContent(); // 居中显示
};

onMounted(() => {
  renderGraph();
});
</script>
